<?php


namespace app\shop\service;


use app\shop\model\user\UserModel;
use Closure;
use think\Model;

abstract class BaseService
{
    public static abstract function getInstance();

    public $model;

    public abstract function __construct();

    /**
     * @return mixed
     */
    public function getModel(): Model
    {
        return $this->model;
    }

    /**
     * 根据ID获取一个值
     * @param $id
     * @param $field
     * @return mixed
     */
    public function getValueById($id, $field)
    {
        $pk = $this->getPk();
        return $this->getModel()->where($pk, $id)->value($field);

    }

    /**
     * 根据ID获取一行数据
     * @param $id
     * @param $field
     * @return array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOneLineById($id, $field = '*')
    {
        $pk = $this->getPk();
        return $this->getModel()->field($field)->where($pk, $id)->find();

    }

    /**
     * 根据键值获取一行数据
     * @param $key
     * @param $val
     * @param string $field
     * @return array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOneLineByField($key, $val, $field = '*')
    {
        return $this->getModel()->field($field)->where($key, $val)->find();
    }

    /**
     * 根据条件获取一条数据
     * @param $where
     * @param string $field
     * @return array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOneLineByWhere($where, $field = '*')
    {
        return $this->getModel()->field($field)->where($where)->find();

    }

    /**
     * 模糊查询一列数据
     * @param $key
     * @param $val
     * @param string $field
     * @param string $fieldKey
     * @return array
     */
    public function getColumnByLike($key, $val, $field = 'id', $fieldKey = '')
    {
        return $this->getModel()->where($key, 'like', '%' . $val . '%')->column($field, $fieldKey);
    }

    /**
     * 根据条件查询一列数据
     * @param $key
     * @param $val
     * @param string $field
     * @param string $fieldKey
     * @return array
     */
    public function getColumnByField($key, $val, $field = 'id', $fieldKey = '')
    {
        return $this->getModel()->where($key, $val)->column($field, $fieldKey);
    }

    /**
     * 添加数据
     * @param $data
     * @return bool
     */
    public function saveData($data)
    {
        return $this->getModel()->save($data);
    }

    /**
     * 获取主键
     * @return array|string
     */
    protected function getPk()
    {
        return $this->getModel()->getPk();
    }

    /**
     * 查询一条数据是否存在
     * @param $map
     * @param string $field
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public function exist($map, string $field = '')
    {
        if (!is_array($map) && empty($field)) {
            $field = $this->getPk();
        }
        $map = !is_array($map) ? [$field => $map] : $map;
        return 0 < $this->getModel()->where($map)->count();
    }

    /**
     * 根据ID更新数据
     * @param $id
     * @param $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateById($id, $data)
    {
        $pk = $this->getPk();
        $model = $this->getModel()->where($pk, $id)->find();
        return $model->save($data);
    }

    /**
     * 根据字段更新一条数据
     * @param $field
     * @param $val
     * @param $upData
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateOneLineByField($field, $val, $upData)
    {
        $model = $this->getModel()->where($field, $val)->find();
        return $model->save($upData);
    }
}